package com.llkj.qingnianguoxue.mine;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.HelpListAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.HelpListBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 帮助中心
 */
public class HelpActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvHelps;
    private HelpListAdapter helpListAdapter;
    private List<HelpListBean.IssueBean> mHelpData;
    private int totalSize,mPage,mCurrentPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        setTitle("帮助中心", true, TITLE_TYPE_IMG, R.drawable.icon_back, true, TITLE_TYPE_TEXT, "意见反馈");

        initView();
        initListener();

        loadWaitDialog();
        onRefresh();
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvHelps = (RecyclerView) findViewById(R.id.recyclerView);
        lvHelps.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        mHelpData = new ArrayList<>();
        helpListAdapter = new HelpListAdapter(R.layout.item_search_result,mHelpData);
        lvHelps.setAdapter(helpListAdapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        helpListAdapter.setOnLoadMoreListener(this);
        lvHelps.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                HelpListBean.IssueBean issueBean = mHelpData.get(i);
                Bundle bundle = new Bundle();
                bundle.putString("id",issueBean.getId());
                bundle.putString("issue",issueBean.getIssue());
                bundle.putString("answer",issueBean.getAnswer());
                startActivity(HelpDetailActivity.class, bundle);
            }
        });

    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        startActivity(FeedBackActivity.class);
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getVideoData();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getVideoData();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, helpListAdapter, null, lvHelps, totalSize);
    }

    private void getVideoData(){
        HttpMethod.userFAQ(this, this, String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_USER_FAQ) {
            HelpListBean hotRankingBean = GsonUtil.GsonToBean(data, HelpListBean.class);
            if (hotRankingBean != null) {
                totalSize = Integer.parseInt(hotRankingBean.getNum());
                if (mPage == 1) {
                    mHelpData.clear();
                    LoadDataHelper.refreshComplete(helpListAdapter, mHelpData, refreshLayout, lvHelps);
                } else {
                    helpListAdapter.setEnableLoadMore(true);
                    helpListAdapter.loadMoreComplete();
                }
                mHelpData.addAll(hotRankingBean.getIssue());
                helpListAdapter.notifyDataSetChanged();
            }
        }
    }
}
